<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
	<head>
		<base href="<%=basePath %>" />
		<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<title></title>
		<link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
		<link rel="stylesheet" href="css/global.css" media="all" />
		<style type="text/css">
			table td,th { font-size: 14px !important;  white-space: nowrap;}
		</style>	
	</head>
	<body>
		<div class="admin-main">

			<blockquote class="layui-elem-quote">
				<a href="javascript:;" class="layui-btn layui-btn" id="add">
					<i class="layui-icon">&#xe608;</i> 添加信息
				</a>
				<a href="javascript:;" class="layui-btn layui-btn" id="import">
					<input type="file" name="filename" class="layui-upload-file" id="importexcel">
				</a>
			<!-- 	<a href="user/exportexcel" class="layui-btn layui-btn">
					<i class="fa fa-shopping-cart" aria-hidden="true"></i> 导出信息
				</a> -->
				<a href="javascript:;" class="layui-btn layui-btn" id="getSelected">
					<i class="fa fa-shopping-cart" aria-hidden="true"></i> 导出信息
				</a>
				<div class="layui-inline">
					<div class="layui-input-inline">
						<input type="text" id="sh-xm" placeholder="姓名.." autocomplete="off" class="layui-input" >
					</div>
				</div>
				<a href="javascript:;" class="layui-btn layui-btn" id="search">
					<i class="layui-icon">&#xe615;</i> 搜索
				</a>
			</blockquote>
			<fieldset class="layui-elem-field">
				<legend>员工数据列表</legend>
				<div class="layui-field-box layui-form">
					<table class="layui-table admin-table">
						<thead>
							<tr>
								<th style="width: 30px;"><input type="checkbox" lay-filter="allselector" lay-skin="primary"></th>
								<th>员工编码</th>
								<th>姓名</th>
								<th>性别</th>
								<th>名族</th>
								<th>籍贯</th>
								<th>年龄</th>
								
								<th>身份证号</th>
								<th>学历</th>
								<!-- <th>学位</th>
								<th>专业</th> -->
								
								<th>政治面貌</th>
								<th>婚姻状况</th>
								<th>入职日期</th>
								<!-- <th>所属项目</th> -->
								<th>所在部门</th>
								<th>岗位名称</th>
								<th>岗位类型</th>
								<th>用工性质</th>
								<!-- <th>联系电话</th> -->
								<!-- <th>照片</th> -->
								<th style="width: 10em;">操作</th>
							</tr>
						</thead>
						<tbody id="content">
						</tbody>
						<tfoot >
							<tr>
								<th colspan="17">
									<div class="admin-table-page">
										<div id="paged" class="page">
										</div>
									</div>
								</th>
							</tr>
						</tfoot>
					</table>
				</div>
			</fieldset>
		
		</div>
		<!--模板-->
		<script type="text/html" id="tpl">
			{{# layui.each(d.list.list, function(index, item){ }}
			<tr>
				<td><input type="checkbox" lay-skin="primary"></td>
				<td>{{ item.ygbm }}</td>
				<td>{{ item.xm }}</td>
				<td>{{ item.xb }}</td>
				<td>{{ item.mz }}</td>
				<td>{{ item.jg }}</td>
 				<td>{{ item.gage }}</td>

				<td>{{ item.sfzh }}</td>
				<td>{{ item.xl }}</td>
				
				
				<td>{{ item.zzmm }}</td>
				<td>{{ item.hyzk }}</td>
				<td>{{ item.rzrq }}</td>
			
				<td>{{ item.szbm }}</td>
				<td>{{ item.gwmc }}</td>
				<td>{{ item.gwlx }}</td>
				<td>{{ item.ygxz }}</td>
			
				
				<td>
				
					<a href="javascript:;" data-name='{{ JSON.stringify(item) }}' data-opt="edit" class="layui-btn layui-btn-mini">编辑</a>
					<a href="javascript:;" data-id="{{ item.gid }}" data-opt="del" class="layui-btn layui-btn-danger layui-btn-mini">删除</a>
				</td>
			</tr>
			{{# }); }}
		</script>
		<script type="text/javascript" src="plugins/layui/layui.js"></script>
		<script>
			layui.config({
				base: 'js/'
			});
			//全局的AJAX访问，处理AJAX清求时SESSION超时
			
			//身份证号算年龄 
            function goage(n){
            	var a = new Date().getFullYear();
            	var b =parseInt(n.substr(6,4));
            	return a-b;
            }
          
			layui.use(['paging', 'form', 'laydate', 'upload','layer'], function() {
				var $ = layui.jquery,
					paging = layui.paging(),
					layerTips = parent.layer === undefined ? layui.layer : parent.layer, //获取父窗口的layer对象
					layer = layui.layer, //获取当前窗口的layer对象
					form = layui.form();
				$.ajaxSetup({
				    contentType:"application/x-www-form-urlencoded;charset=utf-8",
				    complete:function(XMLHttpRequest,textStatus){
				          //通过XMLHttpRequest取得响应头，sessionstatus           
				          var sessionstatus=XMLHttpRequest.getResponseHeader("sessionstatus"); 
				          loginUrl = XMLHttpRequest.getResponseHeader("location"); 
				          if(sessionstatus=='00004'){
				               //这里怎么处理在你，这里跳转的登录页面
				        	 // if( window.top != window.self ){
				  				window.top.location = loginUrl;
				  			//}
				       }
				    }
				});
		          var doajax = function(type, url,_data){  
		              $.ajax({
						type: type,
						url:  url,
						data: _data,
						dataType: 'json',
						success: function(result, status, xhr) {
							if(result.flag){
							 loadtab(data);
							 layer.msg(result.msg);
								}
							}
					});
		          }
		  		//加载字典项
					var doDectAjax=function(){
						$.ajax({type: 'get',url:  'selectdect',
							//data: data.field,
							async: false,dataType: 'json',success: function(result, status, xhr) {
								if(result.flag){
										$(result.list).each(function(i,item){
												switch (item.dtype){
												case "RIGHT" :
													$("select[name='right']").append("<option value='" +item.dictid+"'>"+item.dname+"</option>");
													break;
											 	case "SEX" :
											 		var s = item.temp == 'true' ? 'checked':'';
													$("#sex").append('<input type="radio" name="xb" value="'+item.dname+'" title="'+item.dname+'" '+s+ '/>');	
													break;
												case "MARRY" :
												    var s = item.temp == 'true' ? 'checked':'';
													$("#hyzk").append('<input type="radio" name="hyzk" value="'+item.dname+'" title="'+item.dname+'" '+s+' />');
													break; 
												case "NA" :
													$("select[name='ygxz']").append("<option value='"+item.dname+"'>"+item.dname+"</option>");
													break;
												case "POST" :
													$("select[name='gwlx']").append("<option value='" +item.dname+"'>"+item.dname+"</option>");
													break;
												case "PRO" :
													$("select[name='xmmc']").append("<option value='" +item.dname+"'>"+item.dname+"</option>");
													break;
												case "EDU" :
													$("select[name='xl']").append("<option value='" +item.dname+"'>"+item.dname+"</option>");
													break;
												case "NATION" :
													$("select[name='mz']").append("<option value='" +item.dname+"'>"+item.dname+"</option>");
													break; 
												case "POL" :
													$("select[name='zzmm']").append("<option value='"+item.dname+"'>"+item.dname+"</option>");
													break;
												case "DEGREE" :
													$("select[name='xw']").append("<option value='"+item.dname+"'>"+item.dname+"</option>");
													break;
												default:
													break;
													}
											});
									}
								}
						});
						}
			var data = {};
			var loadtab = function(data){ 
					paging.init({
					url: 'user/findusers', //地址
					elem: '#content', //内容容器
					params: data,
					type: 'POST',
					tempElem: '#tpl', //模块容器
					pageConfig: { //分页参数配置
						elem: '#paged', //分页容器
						pageSize: 10 //分页大小
					},
					success: function() { //渲染成功的回调
						//alert('渲染成功');
					},
					fail: function(msg) { //获取数据失败的回调
						//alert('获取数据失败')
					},
					complate: function() { //完成的回调
						//alert('处理完成');
						//重新渲染复选框
						form.render('checkbox');
						form.on('checkbox(allselector)', function(data) {
							var elem = data.elem;

							$('#content').children('tr').each(function() {
								var $that = $(this);
								//全选或反选
								$that.children('td').eq(0).children('input[type=checkbox]')[0].checked = elem.checked;
								form.render('checkbox');
							});
						});

						//绑定所有编辑按钮事件						
						$('#content').children('tr').each(function() {
							var $that = $(this);
							$that.children('td:last-child').children('a[data-opt=edit]').on('click', function() {
								//JSON.parse(jsonstr); //可以将json字符串转换成json对象 
								//JSON.stringify(jsonobj); 
								//layer.msg($(this).data('name').xm);
								//弹出编辑窗口
								loadUserform($(this).data('name'));
								});
							$that.children('td:last-child').children('a[data-opt=del]').on('click', function() {
								//JSON.parse(jsonstr); //可以将json字符串转换成json对象 
								//JSON.stringify(jsonobj); 
								var id = $(this).data('id');
								layer.confirm('确定删除本条数据吗 ?', {icon: 7, title:'提示'}, function(index){
									  doajax('post', 'user/delete',{id:id});
									  layer.close(index);
									}); 
								
								});
						});
					}
				});
			}
	/* 		jQuery.download = function(url, method, filedir){
			    jQuery('<form action="'+url+'" method="'+(method||'post')+'">' +  // action请求路径及推送方法
			                '<input type="text" name="filedir" value="'+filedir+'"/>'+
			            '</form>')
			    .appendTo('body').submit().remove();
			}; */
			loadtab(data);
				//获取所有选择的列
				$('#getSelected').on('click', function() {
					var names = '';
					$('#content').children('tr').each(function() {
						var $that = $(this);
						var $cbx = $that.children('td').eq(0).children('input[type=checkbox]')[0].checked;
						if($cbx) {
							var n = $that.children('td:last-child').children('a[data-opt=edit]').data('name').infoid;
							names += n + ',';
							if(!names) {layer.msg("请至少选择一项");
							}else{
								}
						}
					});

					$.get('temp/check-col.html', null, function(form) {
						 addBoxIndex = layer.open({
							type: 1,
							title: '下载设置',
							content: form,
							btn: ['下载', '取消'],
							shade: false,
							offset: ['100px', '30%'],
							area: ['600px', '400px'],
							zIndex: 19950924,
							maxmin: true,
							yes: function(index) {
								//触发表单的提交事件
								//设置提交地址
								$('form.layui-form').find('button[lay-filter=edit]').click();
							},
							full: function(elem) {
								var win = window.top === window.self ? window : parent.window;
								$(win).on('resize', function() {
									var $this = $(this);
									elem.width($this.width()).height($this.height()).css({
										top: 0,
										left: 0
									});
									elem.children('div.layui-layer-content').height($this.height() - 95);
								});
							},
							success: function(layero, index) {
								var ltab ;//查询对应的列名和注释
								$.ajax({
									type: 'post',
									url:  'user/colsandcoms',
									data: {tab: 'G_USER_INFO'},
									async: false,
									dataType: 'json',
									success: function(result, status, xhr) {
										if(result.flag){

											latab = result.list;	
											}
										}
								});

								var html = "";
								   $(latab).each(function(i,item){

										html += "<input type='checkbox' name='col'  data-val ='"+JSON.stringify(item)+"' title='"+item.comments+"' > ";
										
									   });
								 $(layero).find('#check-col').html(html);

								 var form  = layui.form();
								 form.render();
								 
								 form.on('submit(edit)', function(data) {
                                        var cols="",coms="";
									     $(layero).find('input[name="col"]').each(function(i, item){
										     if(item.checked){
										     cols += $(item).data('val').column_name+",";
										     coms += $(item).data('val').comments+",";
										     }
										     });
									     var url = 'user/exportexcel?tabname='+data.field.tabname+'&titlename='+data.field.titlename+'&cols='+cols+'&coms='+coms+'&names='+names;
									     layer.close(index);
									     window.location.href=url;
									     return false;
											})
									
								},
							end: function() {
									addBoxIndex = -1;
								}
							});
						});

					
				});

				$('#search').on('click', function() {
					//parent.layer.alert('你点击了搜索按钮');
					data = {xm: $('#sh-xm').val()};
					loadtab(data);
					
				});

				layui.upload({
				    url: 'user/importexcel',
				    type: 'file'	,
				    ext: 'xls',
				    title: '信息导入（xls）'
				    ,elem: '#importexcel' //指定原始元素，默认直接查找class="layui-upload-file"
				    ,method: 'post' //上传接口的http类型
				    ,success: function(res){
					    if(res.code == 0){
					    	layer.msg(res.msg);
					    }
					}
				 });
				 
				// 部门职位
				var addorganiz = function(o_data, o){
					//加载前清空select
					$(o).empty();
					$.ajax({
						type: 'get',
						url:  o_data.url,
						data: o_data,
						async: false,
						dataType: 'json',
						success: function(result, status, xhr) {
							if(result.flag){
								 $(o).append("<option value=''></option>");
								$(result.list).each(function(i,item){
							      $(o).append("<option value='" +item.bmbh+"'>"+item.oname+"</option>");
								});
								}
							}
					});
				}
			//********************************
             var loadUserform = function (item){

                 
				$.get('temp/edit-form.html', null, function(form) {
						addBoxIndex = layer.open({
							type: 1,
							title: '修改表单',
							content: form,
							btn: ['保存', '取消'],
							shade: false,
							offset: ['100px', '30%'],
							area: ['600px', '400px'],
							zIndex: 19950924,
							maxmin: true,
							yes: function(index) {
								//触发表单的提交事件
								//设置提交地址
								$('form.layui-form').find('button[lay-filter=edit]').click();
							},
							full: function(elem) {
								var win = window.top === window.self ? window : parent.window;
								$(win).on('resize', function() {
									var $this = $(this);
									elem.width($this.width()).height($this.height()).css({
										top: 0,
										left: 0
									});
									elem.children('div.layui-layer-content').height($this.height() - 95);
								});
							},
							success: function(layero, index) {
								//弹出窗口成功后渲染表单
							   doDectAjax();
								var a,b;
								a = $("select[name='szbmid']");
								b =	$("select[name='gwid']");
								
								var o_data = {level:"B", url: 'organiz/findbylevl', pid: 0};
								addorganiz(o_data, a);
								o_data.level = "C";
								addorganiz(o_data, b);

								

								//给表单赋值
								/**********/
								$("input[name='id']").val(item.gid);
								$("input[name='loginname']").val(item.loginname);
								$("input[name='password']").addClass('layui-unselect');
								$("select[name='right']").val(item.right);
								$("input[name='ygbm']").val(item.ygbm);
								$("input[name='xm']").val(item.xm);
								$(":radio[name='xb'][value='"+item.xb+"']").prop("checked", "checked");
								//$("input[name='mz']").val(item.mz);
								$("input[name='jg']").val(item.jg);
								$("input[name='gage']").val(item.gage);
								$("input[name='sfzh']").val(item.sfzh);
								$("input[name='zcdw']").val(item.zcdw);
								$("input[name='zy']").val(item.zy);
							//	$("input[name='xw']").val(item.xw);
							//	$("input[name='xl']").val(item.xl);
								$("input[name='zc']").val(item.zc);
							//	$("input[name='zzmm']").val(item.zzmm);
								$(":radio[name='hyzk'][value='"+item.hyzk+"']").prop("checked", "checked");
								$("input[name='rzrq']").val(item.rzrq);

								$("select[name='szbmid']").val(item.szbmid);
								$("select[name='gwid']").val(item.gwid);
								
								$("select[name='gwlx']").val(item.gwlx);
								$("select[name='ygxz']").val(item.ygxz);
								$("select[name='xmmc']").val(item.xmmc);
								$("select[name='mz']").val(item.mz);
								$("select[name='xl']").val(item.xl);
								$("select[name='xw']").val(item.xw);
								$("select[name='zzmm']").val(item.zzmm);
								
								$("input[name='lxdh']").val(item.lxdh);
								$("#LAY_demo_upload").attr('src', item.zp);
								$("#zp").val(item.zp);
								
								
								/************/	
								var form = layui.form();
								form.render();
								layui.upload({
								    url: '_upload'
								    ,elem: '#test' //指定原始元素，默认直接查找class="layui-upload-file"
								    ,ext: 'jpg|png|gif'
									,title: '文件上传（jpg|png|gif）'
								    ,method: 'post' //上传接口的http类型
								    ,success: function(res){
									    if(res.isSuccess){
									  $('#zp').val(res.url);   
								      LAY_demo_upload.src = res.url;
									    }
									}
								 });
								//部门级联操作
								form.on('select(depart)',function(data){
									o_data.url = 'organiz/findbypid'
									o_data.pid = data.value;
									addorganiz(o_data, b);
									form.render("select");
								});
								
								form.on('submit(edit)', function(data) {
									$.ajax({
										type: 'post',
										url:  'user/update',
										data: data.field,
										dataType: 'json',
										success: function(result, status, xhr) {
											if(result.flag){
												layerTips.open({
													title: '提示',
													type: 1,
													content: result.msg,
													area: ['500px', '300px'],
													btn: '关闭并刷新',
													yes: function(index, layero) {
														layerTips.msg('你点击了关闭并刷新');
														layerTips.closeAll();
														location.reload(); //刷新
													}

												}); 
												}
											}
									});
									
									return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。									
								});
								//console.log(layero, index);
							},
							end: function() {
								addBoxIndex = -1;
							}
						});
					});
				}

				//**************************************************************end
				var addBoxIndex = -1;
				$('#add').on('click', function() {
					if(addBoxIndex !== -1)
						return;
					//本表单通过ajax加载 --以模板的形式，当然你也可以直接写在页面上读取
					$.get('temp/edit-form.html', null, function(form) {
						addBoxIndex = layer.open({
							type: 1,
							title: '添加表单',
							content: form,
							btn: ['保存', '取消'],
							shade: false,
							offset: ['100px', '30%'],
							area: ['600px', '400px'],
							zIndex: 19950924,
							maxmin: true,
							yes: function(index) {
								//触发表单的提交事件
								$('form.layui-form').find('button[lay-filter=edit]').click();
							},
							full: function(elem) {
								var win = window.top === window.self ? window : parent.window;
								$(win).on('resize', function() {
									var $this = $(this);
									elem.width($this.width()).height($this.height()).css({
										top: 0,
										left: 0
									});
									elem.children('div.layui-layer-content').height($this.height() - 95);
								});
							},
							success: function(layero, index) {
								//弹出窗口成功后渲染表单
								$.ajax({
										type: 'get',
										url:  'selectdect',
										//data: data.field,
										async: false,
										dataType: 'json',
										success: function(result, status, xhr) {
											if(result.flag){
													$(result.list).each(function(i,item){
															switch (item.dtype){
															case "RIGHT" :
																$("select[name='right']").append("<option value='" +item.dictid+"'>"+item.dname+"</option>");
																break;
														 	case "SEX" :
														 		 var s = item.temp == 'true' ? 'checked':'';
																$("#sex").append('<input type="radio" name="xb" value="'+item.dname+'" title="'+item.dname+'" '+s+ ' >');
																//$("select[name='xb']").append("<option value='"+item.dname+"'>"+item.dname+"</option>");
																break;
															case "MARRY" :
															    var s = item.temp == 'true' ? 'checked':'';
																$("#hyzk").append('<input type="radio" name="hyzk" value="'+item.dname+'" title="'+item.dname+'" '+s+' >');
																break; 
															case "NA" :
																$("select[name='ygxz']").append("<option value='"+item.dname+"'>"+item.dname+"</option>");
																break;
															case "POST" :
																$("select[name='gwlx']").append("<option value='" +item.dname+"'>"+item.dname+"</option>");
																break;
															case "PRO" :
																$("select[name='xmmc']").append("<option value='" +item.dname+"'>"+item.dname+"</option>");
																break;
															case "EDU" :
																$("select[name='xl']").append("<option value='" +item.dname+"'>"+item.dname+"</option>");
																break;
															case "NATION" :
																$("select[name='mz']").append("<option value='" +item.dname+"'>"+item.dname+"</option>");
																break; 
															case "POL" :
																$("select[name='zzmm']").append("<option value='"+item.dname+"'>"+item.dname+"</option>");
																break;
															case "DEGREE" :
																$("select[name='xw']").append("<option value='"+item.dname+"'>"+item.dname+"</option>");
																break;
															default:
																break;
																}
														});
												}
											}
									});
								var a,b;
								a = $("select[name='szbmid']");
								b =	$("select[name='gwid']");
								
								var o_data = {level:"B", url: 'organiz/findbylevl', pid: 0};
								addorganiz(o_data, a);
								o_data.level = "C";
								addorganiz(o_data, b);
								
								var form = layui.form();
								form.render();
								layui.upload({
								    url: '_upload'
								    ,elem: '#test' //指定原始元素，默认直接查找class="layui-upload-file"
								    ,ext: 'jpg|png|gif'
								    ,title: '文件上传（jpg|png|gif）'
								    ,method: 'post' //上传接口的http类型
								    ,success: function(res){
									    if(res.isSuccess){
									  $('#zp').val(res.url);   
								      LAY_demo_upload.src = res.url;
									    }
									}
								 });
								form.verify({
									  password: function(value){
										 var reg = /^[A-Za-z0-9]{6,20}$/;
									    if(!reg.test(value)){
									    	layerTips.msg('请正确输入！');
									    	return "";
									    }
									  }
									});   
								
								 
								form.on('select(depart)',function(data){
									o_data.url = 'organiz/findbypid'
									o_data.pid = data.value;
									addorganiz(o_data, b);
									form.render("select");
									});
								
								form.on('submit(edit)', function(data) {
									console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
									console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
									console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
									//调用父窗口的layer对象
								/* 	layerTips.open({
										title: '这里面是表单的信息',
										type: 1,
										content: JSON.stringify(data.field),
										area: ['500px', '300px'],
										btn: ['关闭并刷新', '关闭'],
										yes: function(index, layero) {
											layerTips.msg('你点击了关闭并刷新');
											layerTips.close(index);
											location.reload(); //刷新
										}

									}); */

									$.ajax({
										type: 'post',
										url:  'user/add',
										data: data.field,
										dataType: 'json',
										success: function(result, status, xhr) {
											if(result.flag){
												
												layer.closeAll();
												location.reload(); //刷新

												}else{
													layerTips.msg(result.msg);
													}
											}
									});
									
									return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。									
								});
								//console.log(layero, index);
							},
							end: function() {
								addBoxIndex = -1;
							}
						});
					});
				});

				$('#import').on('click', function() {
					var that = this;

				});
			});
		</script>
	</body>
	</body>

</html>